#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Weather;
use strict;
use warnings;
use XML::Simple;
use LWP::Simple;
use base qw(App::Modular::Module);

print ":: Weather module loaded.\n";

# Events we want to receive.
sub events { return "PRIVMSG"; }

sub help {
        return (
                "Use Google Weather XML to retrieve your forecast.",
                "weather <zip code/city>"
        );
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        my $help = $e->robo->mod("Helper") || return;

        # Admin commands for slashdot module.
        if ($help->trigger_switch("weather", $e)) {
                return;
        }

        # Weather trigger.
        if (my $h = $help->trig("weather", $e, 1)) {
                read_weather($e, $h->{data});
        }
}

# Read the Google weather XML feed.
sub read_weather
{
        my ($e, $input) = @_;

        # Download the xml content:
        # The get() function downloads any text file from the web
        # and will store it in a variable.
        my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0 (X11; U; Linux i686; ".
                                              "en-US; rv:1.9.0.2) ".
                                              "Gecko/2008092313 Ubuntu/8.04 ".
                                              "(hardy) Firefox/3.1");

        my $response = $ua->get("http://www.google.com/ig/api?weather=".$input);
        if (!$response->is_success) {
                $e->s("PRIVMSG ".$e->dest." :\002weather\002: Could not download weather XML!");
                return;
        }

        my $content = $response->content;

        # Open the XML feed.
        my $xml = XMLin($content) or return;

        my $city = $xml->{weather}->{forecast_information}->{city}->{data};
        my $cur_temp = $xml->{weather}->{current_conditions}->{temp_f}->{data};
        my $cur_wind = $xml->{weather}->{current_conditions}->{wind_condition}->{data};
        my $cur_skies = $xml->{weather}->{current_conditions}->{condition}->{data};
        my $high = $xml->{weather}->{forecast_conditions}->[0]->{high}->{data};
        my $low = $xml->{weather}->{forecast_conditions}->[0]->{low}->{data};
        my $condition = $xml->{weather}->{forecast_conditions}->[0]->{condition}->{data};

        $e->s("PRIVMSG ".$e->dest." :\002weather\002: ".$city.
              " - Current temp: ".$cur_temp."F - ".
              $cur_wind.
              " - High: ".$high." - Low: ".$low.
              " - Condition: ".$condition);
}

1;
